package com.rh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rh.entity.EduChapter;
import com.rh.entity.EduVideo;
import com.rh.entity.chapter.ChapterVo;
import com.rh.entity.chapter.VideoVo;
import com.rh.mapper.EduChapterMapper;
import com.rh.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rh.service.EduVideoService;
import com.rh.exception.EducationException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程 服务实现类
 * @author rh
 * @since 2021-09-24
 */
@Service
@Transactional
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    //注入小结的业务类
    @Resource
    private EduVideoService videoService;
    /**
     * 根据课程id查询获取章节信息
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.先查询出来所有的章节信息
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(chapterWrapper);
        //2.在查出来所有的小结
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = videoService.list(videoWrapper);

        //创建一个集合来存放最终数据
        List<ChapterVo>finalList=new ArrayList<>();
        //3.遍历查询出来章节的信息进行封装
        for (int i = 0; i < eduChapters.size(); i++) {
            //每个章节信息
            EduChapter eduChapter = eduChapters.get(i);
            //把eduChapters对象复制到eduChapter
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //复制完毕后把chapterVo放到最终集合中
            finalList.add(chapterVo);
            //获取章节id
            String chapterId = eduChapter.getId();
            //创建一个集合用来封装小节
            List<VideoVo>finalVideoList=new ArrayList<>();
            //4.遍历查询出来的小结信息进行封装
            for (int j = 0; j < eduVideos.size(); j++) {
                //得到每个小结
                EduVideo eduVideo = eduVideos.get(j);
                //判断如果查询出来的每个小结中的章节id和每个章节中的章节id相同，则是同一章节下的小结
                if(eduVideo.getChapterId().equals(chapterId)){
                    //如果是同一章节下的小结就进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放到小结集合中
                    finalVideoList.add(videoVo);
                }
            }
            //把封装之后小结的集合放到章节集合中
            chapterVo.setChildren(finalVideoList);
        }
        return finalList;
    }

    /**
     * 删除章节
     * 如果章节下有小节,先删除小节再删除章节
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {
    //根据chapterid查询小节,如果有数据就不进行删除
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id",chapterId);
        //mybaitsPlus中查询数据记录数的方法count
        int count = videoService.count(eduVideoQueryWrapper);
        if(count>0){ //如果count>0说明有数据
           throw new EducationException(20001,"不能进行删除,里面有还有小节~");
        }else { //如果没有数据就调用删除方法删除
            int i = baseMapper.deleteById(chapterId);
            //如果成功1>0, i>0就是true,i<0就是false
            return i>0;
        }
    }
    /**
     * 根据课程id删除章节
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper= new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
